package com.example.himalaya2.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya2.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import org.jetbrains.annotations.NotNull;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class DetailListAdapter  extends RecyclerView.Adapter<DetailListAdapter.InnerHolder> {

    private List<Track> mDetailData = new ArrayList<>();

    //格式化时间
    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");

    ItemClickListener mItemClickListener = null;

    @NonNull
    @NotNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_detail,parent,false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull DetailListAdapter.InnerHolder holder, int position) {
        // 找到控件，设置数据
        View itemView = holder.itemView;
        // 顺序id
        TextView orderTv = itemView.findViewById(R.id.order_text);
        // 标题id
        TextView titleTv = itemView.findViewById(R.id.detail_item_title);
        // 播放次数
        TextView playCountTv = itemView.findViewById(R.id.detail_item_play_count);
        // 时长
        TextView durationTv = itemView.findViewById(R.id.detail_item_duration);
        // 更新日期
        TextView upDateTimeTv = itemView.findViewById(R.id.detail_item_update_time);

        // 设置数据
        Track track = mDetailData.get(position);
        orderTv.setText(track.getOrderNum()+1+"");
        titleTv.setText(track.getTrackTitle());
        playCountTv.setText(track.getPlayCount()+"");
        durationTv.setText(mDurationFormat.format(track.getDuration()*1000));
        upDateTimeTv.setText(mSimpleDateFormat.format(track.getUpdatedAt()));
        // 设置item的点击事件
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // todo
                // Toast.makeText(view.getContext(),track.getTrackTitle()+"",Toast.LENGTH_SHORT).show();
                if (mItemClickListener != null) {
                    // 需要有列表和在列表上的位置
                    mItemClickListener.onItemClicked(mDetailData,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> tracks) {
        mDetailData.clear();  // 先清除数据
        mDetailData.addAll(tracks);
        notifyDataSetChanged();  // 更新ui
    }

    public class InnerHolder extends RecyclerView.ViewHolder{
        public InnerHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }
    }

    public void setItemClickListener(ItemClickListener listener){
        this.mItemClickListener = listener;
    }
    public interface ItemClickListener{
        void onItemClicked(List<Track> detailData, int position);
    }
}
